var $,tab,dataStr,layer,indexAll;
layui.config({
	base : "/ssm/static/system/js/"
}).extend({
	"bodyTab" : "bodyTab"
});
indexAll=layui.use(['bodyTab','form','element','layer','jquery'],function(){
	
	var form = layui.form,
		element = layui.element;
		$ = layui.jquery;
    	layer = parent.layer === undefined ? layui.layer : top.layer;
		tab = layui.bodyTab({
			openTabNum : "10",  //最大可打开窗口数量
			url : "/sys/menu/findByPid" //获取菜单json地址
		});

	//写死左边的菜单栏,估计用不上菜单控制
	var menu_data = [
	    {   id: 19, pid: 14, menuName: "商品管理", linkUrl: "", sort: null, level: 1, status: null, parentCode: null,pid:14,sort:null,status:null,
	        childSysMenus:[
	        	{id: 1, pid: 19, menuName: "商品分类", linkUrl: "/ssm/system/classes/classesList.html", sort: 0, level: 2, status: 1},
                {id: 3, pid: 19, menuName: "商品管理", linkUrl: "/ssm/system/product/productList.html", sort: 0, level: 2, status: 1},
            ]
	    },
	    {   id: 16, pid: 14, menuName: "订单管理", linkUrl: "", sort: null, level: 1, status: null, parentCode: null,pid:14,sort:null,status:null,
	        childSysMenus:[
	        	{id: 1, pid: 19, menuName: "订单管理", linkUrl: "/ssm/system/order/orderList.html", sort: 0, level: 2, status: 1},
            ]
	    },
	    {   id: 21, pid: 14, menuName: "广告管理", linkUrl: "", sort: null, level: 1, status: null, parentCode: null,pid:14,sort:null,status:null,
	        childSysMenus:[
                {id: 1, pid: 20, menuName: "首页广告管理", linkUrl: "/ssm/system/banner/bannerList.html", sort: 0, level: 2, status: 1},
	        ]
	    },
	    {   id: 17, pid: 14, menuName: "会员管理", linkUrl: "", sort: null, level: 1, status: null, parentCode: null,pid:14,sort:null,status:null,
	        childSysMenus:[
                {id: 1, pid: 17, menuName: "商城会员信息", linkUrl: "/ssm/system/consumer/consumerList.html", sort: 0, level: 2, status: 1},
	        ]
	    },
	    {   id: 20, pid: 14, menuName: "系统管理", linkUrl: "", sort: null, level: 1, status: null, parentCode: null,pid:14,sort:null,status:null,
	        childSysMenus:[
                {id: 1, pid: 20, menuName: "账号信息", linkUrl: "/ssm/system/user/userList.html", sort: 0, level: 2, status: 1},
	        ]
	    }
	]
	dataStr = menu_data;
    tab.render();


	//通过顶部菜单获取左侧二三级菜单   注：此处只做演示之用，实际开发中通过接口传参的方式获取导航数据
	function getData(pid){
		$.post(tab.tabConfig.url+"?pid="+pid,function(data){
			dataStr = data.body;
			tab.render();
		})
	}
	//页面加载时判断左侧菜单是否显示
	//通过顶部菜单获取左侧菜单
	$(".topLevelMenus li,.mobileTopLevelMenus dd").click(function(){
		if($(this).parents(".mobileTopLevelMenus").length != "0"){
			$(".topLevelMenus li").eq($(this).index()).addClass("layui-this").siblings().removeClass("layui-this");
		}else{
			$(".mobileTopLevelMenus dd").eq($(this).index()).addClass("layui-this").siblings().removeClass("layui-this");
		}
		$(".layui-layout-admin").removeClass("showMenu");
		$("body").addClass("site-mobile");
		getData($(this).data("menu"));
		//渲染顶部窗口
		tab.tabMove();
	})

	//隐藏左侧导航
	$(".hideMenu").click(function(){
		if($(".topLevelMenus li.layui-this a").data("url")){
			layer.msg("此栏目状态下左侧菜单不可展开");  //主要为了避免左侧显示的内容与顶部菜单不匹配
			return false;
		}
		$(".layui-layout-admin").toggleClass("showMenu");
		//渲染顶部窗口
		tab.tabMove();
	})

	//通过顶部菜单获取左侧二三级菜单   注：此处只做演示之用，实际开发中通过接口传参的方式获取导航数据
	//getData("contentManagement");

	//手机设备的简单适配
    $('.site-tree-mobile').on('click', function(){
		$('body').addClass('site-mobile');
	});
    $('.site-mobile-shade').on('click', function(){
		$('body').removeClass('site-mobile');
	});

	// 添加新窗口
	$("body").on("click",".layui-nav .layui-nav-item a:not('.mobileTopLevelMenus .layui-nav-item a')",function(){
		//如果不存在子级
		if($(this).siblings().length == 0){
			addTab($(this));
			$('body').removeClass('site-mobile');  //移动端点击菜单关闭菜单层
		}
		$(this).parent("li").siblings().removeClass("layui-nav-itemed");
	})

	//清除缓存
	$(".clearCache").click(function(){
		window.sessionStorage.clear();
        window.localStorage.clear();
        var index = layer.msg('清除缓存中，请稍候',{icon: 16,time:false,shade:0.8});
        setTimeout(function(){
            layer.close(index);
            layer.msg("缓存清除成功！");
        },1000);
    })

	//刷新后还原打开的窗口
    if(cacheStr == "true") {
        if (window.sessionStorage.getItem("menu") != null) {
            menu = JSON.parse(window.sessionStorage.getItem("menu"));
            curmenu = window.sessionStorage.getItem("curmenu");
            var openTitle = '';
            for (var i = 0; i < menu.length; i++) {
                openTitle = '';
                if (menu[i].icon) {
                    if (menu[i].icon.split("-")[0] == 'icon') {
                        openTitle += '<i class="seraph ' + menu[i].icon + '"></i>';
                    } else {
                        openTitle += '<i class="layui-icon">' + menu[i].icon + '</i>';
                    }
                }
                openTitle += '<cite>' + menu[i].title + '</cite>';
                openTitle += '<i class="layui-icon layui-unselect layui-tab-close" data-id="' + menu[i].layId + '">&#x1006;</i>';
                element.tabAdd("bodyTab", {
                    title: openTitle,
                    content: "<iframe src='" + menu[i].href + "' data-id='" + menu[i].layId + "'></frame>",
                    id: menu[i].layId
                })
                //定位到刷新前的窗口
                if (curmenu != "undefined") {
                    if (curmenu == '' || curmenu == "null") {  //定位到后台首页
                        element.tabChange("bodyTab", '');
                    } else if (JSON.parse(curmenu).title == menu[i].title) {  //定位到刷新前的页面
                        element.tabChange("bodyTab", menu[i].layId);
                    }
                } else {
                    element.tabChange("bodyTab", menu[menu.length - 1].layId);
                }
            }
            //渲染顶部窗口
            tab.tabMove();
        }
    }else{
		window.sessionStorage.removeItem("menu");
		window.sessionStorage.removeItem("curmenu");
	}
	
	
	




function initWindowData(pid){
	$.post(tab.tabConfig.url+"?pid="+pid,function(data){
		dataStr = data.body;
		tab.render();
	});
}


//打开新窗口
function addTab(_this){
	tab.tabAdd(_this);
}



//图片管理弹窗
function showImg(){
    $.getJSON('json/images.json', function(json){
        var res = json;
        layer.photos({
            photos: res,
            anim: 5
        });
    });
}



getUserInfo();
function  getUserInfo(){
    $.ajax({
        url:"/ssm/loginInfo.do",
        type:"POST",
        dataType:"json",
        success:function(data){
           var info = data.body;
           if(info==null){
        	   location.href='/ssm/login.html';
        	   return;
           }
           $("#userRealName").text(info.nick_name);
           var listMenuGroup = info.listMenuGroup;
           for(var idx in listMenuGroup){
                var menuGroup = listMenuGroup[idx];
                var icon = menuGroup.iconCls;
                var menuGroupTitle = menuGroup.menuName;
                var menuGroupId = menuGroup.id;

                var ddElement = document.createElement("dd");
                var aElement = document.createElement("a");
                var liElement = document.createElement("li");
                var iElement = document.createElement("i");
                var citeElement = document.createElement("cite");
                var spanElement = document.createElement("span");

                spanElement.innerHTML=menuGroupTitle;
                citeElement.appendChild(spanElement);
                iElement.setAttribute("class",icon);
                aElement.appendChild(citeElement);
                aElement.appendChild(iElement);
                aElement.setAttribute("href","javascript:;");
                ddElement.appendChild(aElement);
                ddElement.setAttribute("data-menu",menuGroupId);

                document.getElementById("mobileTopLevelMenus").appendChild(ddElement);

                aElement = document.createElement("a");
                liElement = document.createElement("li");
                iElement = document.createElement("i");
                citeElement = document.createElement("cite");
                spanElement = document.createElement("span");

                citeElement.innerHTML =menuGroupTitle;
                iElement.setAttribute("class",icon);
                aElement.appendChild(citeElement);
                aElement.appendChild(iElement);
                aElement.setAttribute("href","javascript:;");
                liElement.setAttribute("data-menu",menuGroupId);
                liElement.setAttribute("class","layui-nav-item");
                liElement.setAttribute("pc",null);
                liElement.appendChild(aElement);
                document.getElementById("topLevelMenus").appendChild(liElement);
           }
           var spanElement = document.createElement("span");
           spanElement.setAttribute("class","layui-nav-bar");
            document.getElementById("topLevelMenus").appendChild(spanElement);

            $(document).on('click', '.topLevelMenus li,.mobileTopLevelMenus dd', function(data) {
               if($(this).parents(".mobileTopLevelMenus").length != "0"){
                        $(".topLevelMenus li").eq($(this).index()).addClass("layui-this").siblings().removeClass("layui-this");
                    }else{
                        $(".mobileTopLevelMenus dd").eq($(this).index()).addClass("layui-this").siblings().removeClass("layui-this");
                    }
                    $(".layui-layout-admin").removeClass("showMenu");
                    $("body").addClass("site-mobile");
                    getData($(this).data("menu"));
                    //渲染顶部窗口
                    tab.tabMove();

            });

    } })
 }
$(".signOut").click(function(){
         $.post("/ssm/logout.do",function(data){
             if(data.code=="01"){
                 location.href="/ssm/login.html";
             }else{
                 top.layer.msg(data.message);
             }
         })
      })

 });